// pages/search/searchHistory.js
import {
  list
} from '../../../../api/seq'
import {
  list as productList
} from '../../../../api/product'
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isTitleShow: {
      type: Boolean
    },
    status: {
      type: Boolean
    },
    historyList: {
      type: Array
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    brandList:[],
    isProduct: false,
    products: [],
    productName: '',
    isShow: false,
    showSearch: false,
    status: false,
    seqList: []
  },
  created() {
    this.loadSq()
  },
  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached() {
      console.log(this.data.isTitleShow)
    }
  },
  methods: {
    loadProducts(e) {
      var hash = {};
      if (e.currentTarget.dataset.brandname.length > 0) {
        this.data.productName = e.currentTarget.dataset.brandname
      }
      this.setData({
        productName: this.data.productName
      })
      productList({
        mchId: app.globalData.mchId,
        name: this.data.productName
      }).then(res => {

        this.data.products = res.data.data
        this.data.products.forEach(item=>{
          let brand={
            brandId:item.brandId,
            brandName:item.brandName,
            isActive:false
          }
          this.data.brandList.push(brand)
        })
      	this.data.brandList = this.data.brandList.reduce((item, next) => {
					hash[next.brandName] ? '' : hash[next.brandName] = true && item.push(next);
					return item
        }, [])
        if (this.data.products.length > 0) {
          this.data.isProduct = true
        } else {
          this.data.isProduct = false
        }
        this.data.showSearch = true,
          this.setData({
            brandList:this.data.brandList,
            isProduct: this.data.isProduct,
            showSearch: this.data.showSearch,
            products: this.data.products
          })
          console.log(this.data.brandList)
        this.triggerEvent('loadProduct', {
          brandList:this.data.brandList,
          productName: this.data.productName,
          products: this.data.products,
          showSearch: this.data.showSearch,
          isProduct: this.data.isProduct
        })
      })
    },
    loadProduct(e) {
      var hash = {};
      if (e.currentTarget.dataset.name.length > 0) {
        this.data.productName = e.currentTarget.dataset.name
      }
      this.setData({
        productName: this.data.productName
      })
      productList({
        mchId: app.globalData.mchId,
        name: this.data.productName
      }).then(res => {
        this.data.products = res.data.data
        this.data.products.forEach(item=>{
          let brand={
            brandId:item.brandId,
            brandName:item.brandName,
            isActive:false
          }
          this.data.brandList.push(brand)
        })
        this.data.brandList = this.data.brandList.reduce((item, next) => {
					hash[next.brandName] ? '' : hash[next.brandName] = true && item.push(next);
					return item
        }, [])
        this.data.showSearch = true
        if (this.data.products.length > 0) {
          this.data.isProduct = true
        } else {
          this.data.isProduct = false
        }
        this.setData({
          brandList:this.data.brandList,
          isProduct: this.data.isProduct,
          showSearch: this.data.showSearch,
          products: this.data.products
        })
        this.triggerEvent('loadProduct', {
          brandList:this.data.brandList,
          productName: this.data.productName,
          products: this.data.products,
          showSearch: this.data.showSearch,
          isProduct: this.data.isProduct
        })
      })
    },
    isClick() {
      this.data.isShow = !this.data.isShow

      this.setData({
        isShow: this.data.isShow
      })
    },
    loadSq() {
      list({
        mchId: app.globalData.mchId
      }).then(res => {
        this.data.seqList = res.data
        this.setData({
          seqList: this.data.seqList
        })
      })
    },
    delHistory() {
      this.triggerEvent("transfer")
    }
  }
})